package inesv.activeMQ.activeMQ_springboot;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import java.io.Serializable;

/**
 * 消费者
 *
 */
@Component
public class Consumer {

    /**
     * destination:是消息目的地
     * containerFactory：容器类型，需要指定JmsConfiguration类中的Bean
     * @param msg
     */
    @JmsListener(destination=DestinationList.test_queue,containerFactory = "jmsListenerContainerQueue")
    public void msgConsumer(Serializable msg){
        Email email =  (Email)msg;
        System.out.println("Queue消费者接收到消息："+ email.toString());
    }


    @JmsListener(destination=DestinationList.test_topic,containerFactory = "jmsListenerContainerTopic")
    public void msgConsumerTopic1(Serializable msg, Session session ){
        try {
            Email email =  (Email)msg;
            System.out.println("Topic消费者接收到消息："+ email.toString());
            int i = 1/0;//模拟异常
            System.out.println("消息处理成功！！！");
        }catch (Exception e){
            try {
                session.rollback();//触发消息重发
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * destination:是消息目的地
     * containerFactory：容器类型，需要指定JmsConfiguration类中的Bean
     * @param msg
     */
    @JmsListener(destination=DestinationList.schedule_queue,containerFactory = "jmsListenerContainerQueue")
    public void scheduleMsgConsumer(Serializable msg){
        Email email =  (Email)msg;
        System.out.println("Queue消费者接收到消息："+ email.toString());
    }
}
